//
//  RewardVideoVC.m
//  CJModileAdDemo
//
//  Created by JUN on 2022/3/6.
//

#import "RewardVideoVC.h"
#import "CustomShowView.h"
#import <WindMillSDK/WindMillRewardVideoAd.h>
#import <WindMillSDK/WindMillAdRequest.h>
#import <WindMillSDK/WindMillConstant.h>
#import <WindMillSDK/WindMillAdInfo.h>

@interface RewardVideoVC ()<WindMillRewardVideoAdDelegate>

@property (nonatomic,strong) WindMillRewardVideoAd *rewardVideoAd;

@end

@implementation RewardVideoVC

@synthesize customView = _customView;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)dealloc
{
    NSLog(@"事件:%s", __FUNCTION__);
}
 
- (void)loadAdData {
    WindMillAdRequest *request = [WindMillAdRequest request];
    request.userId = @"user_id";
    request.placementId = @"7728449438825905";
    //rewardVideoAd全局对象，不能每次都重新new实例对象，否则预加载功能不生效
    if (self.rewardVideoAd == nil) {
        self.rewardVideoAd = [[WindMillRewardVideoAd alloc] initWithRequest:request];
    }
    self.rewardVideoAd.delegate = self;
    [self.rewardVideoAd loadAdData];
}

- (void)setupUI {
    self.title = @"RewardVideoAd";
    [self.view addSubview:self.customView];
    self.customView.frame = CGRectMake(0, 100, 200, 200);
    self.customView.center = self.view.center;
}

- (void)handleEvent:(NSInteger)eve {
    if (eve == 0) {
        [self loadAdData];
        return;
    }
    [self.rewardVideoAd showAdFromRootViewController:self options:nil];
}

- (CustomShowView *)customView {
    if (!_customView) {
        _customView = [[CustomShowView alloc] init];
        __weak RewardVideoVC *weakSelf = self;
        _customView.event = ^(NSInteger eve) {
            [weakSelf handleEvent:eve];
        };
    }
    return _customView;
}

#pragma mark CJRewardVideoAdDelegate
- (void)rewardVideoAd:(WindMillRewardVideoAd *)rewardVideoAd reward:(WindMillRewardInfo *)reward {
    NSLog(@"事件:%s", __FUNCTION__);
}

- (void)rewardVideoAdDidLoad:(WindMillRewardVideoAd *)rewardVideoAd {
//    NSLog(@"cpm为%ld", rewardVideoAd.adInfo.eCPM);
    [self.customView canShowStatus:true errorMsg:@""];
    NSLog(@"事件:%s", __FUNCTION__);
}

- (void)rewardVideoAd:(WindMillRewardVideoAd *)rewardVideoAd didFinishBidADSource:(WindMillAdInfo *)adInfo {
    NSLog(@"正式的为%ld", rewardVideoAd.adInfo.eCPM);
    NSLog(@"事件:%s", __FUNCTION__);
}

- (void)rewardVideoAdDidLoad:(WindMillRewardVideoAd *)rewardVideoAd didFailWithError:(NSError *)error {
    [self.customView canShowStatus:false errorMsg:error.debugDescription];
    NSLog(@"事件:%s", __FUNCTION__);
}

- (void)rewardVideoAdDidVisible:(WindMillRewardVideoAd *)rewardVideoAd {
    NSLog(@"事件:%s", __FUNCTION__);
}

- (void)rewardVideoAdDidClick:(WindMillRewardVideoAd *)rewardVideoAd {
    NSLog(@"事件:%s", __FUNCTION__);
}

- (void)rewardVideoAdDidClose:(WindMillRewardVideoAd *)rewardVideoAd {
    [self.customView canShowStatus:false errorMsg:@""];
    NSLog(@"事件:%s", __FUNCTION__);
}

@end
